﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DATA.BusinessObjects;

namespace Presentation
{
    public partial class frmBaoCaoDoanhThuTheoLoaiPhong : Form
    {
        public frmBaoCaoDoanhThuTheoLoaiPhong()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLapBaoCao_Click(object sender, EventArgs e)
        {
            dgvChiTietBaoCao.Rows.Clear();

            double tongdoanhthu = 0;
            ArrayList doanhthu = new ArrayList();
            ArrayList tile = new ArrayList();

            IList<Loaiphong> list_lp = LoaiPhongBUS.LayDanhSachLoaiPhong();

            for (int i = 0; i < list_lp.Count; i++)
            {
                double doanhthu_loaiphong = 0;
                IList list_mix = BaoCaoBUS.LayBaoCaoDoanhThuTheoMaLoaiPhong_Thang(list_lp[i].Id.ToString(), int.Parse(cbxThangCanLapBaoCao.SelectedItem.ToString()));
                for (int j = 0; j < list_mix.Count; j++)
                {
                    object[] temp = (object[])list_mix[j];
                    Phieuthuephong ptp = (Phieuthuephong)(temp[0]);
                    doanhthu_loaiphong += ptp.DonGiaThue.Value * Math.Abs(ptp.NgayTraPhong.Value.Subtract(ptp.NgayThuePhong.Value).Days);
                }
                tongdoanhthu += doanhthu_loaiphong;

                doanhthu.Add(doanhthu_loaiphong);
            }

            for (int i = 0; i < doanhthu.Count; i++)
            {
                double tl = 0;
                if (tongdoanhthu != 0)
                    tl = (double)doanhthu[i] * 100 / tongdoanhthu;
                tile.Add(tl);
            }

            for (int i = 0; i < list_lp.Count; i++)
            {
                String[] dong = new string[] { (i + 1).ToString(), list_lp[i].TenLoaiPhong.ToString(), doanhthu[i].ToString(), tile[i].ToString() + "%" };
                dgvChiTietBaoCao.Rows.Add(dong);
            }
            txtTongDoanhThu.Text = tongdoanhthu.ToString();

            #region lưu xuống database
            int thang = int.Parse(cbxThangCanLapBaoCao.SelectedItem.ToString());
            if(DoanhThuTheoLoaiPhongBUS.LayDoanhThuTheoLoaiPhongTheoThang(thang) != null)
            {
                MessageBox.Show("Báo cáo thành công rồi !!!");
                return;
            }

            Doanhthutheoloaiphong dttlp = new Doanhthutheoloaiphong();

            if (DoanhThuTheoLoaiPhongBUS.LayDoanhThuTheoLoaiPhongCuoiCung() != null)
                dttlp.Id = ThamSoBUS.TaoMaTangTuDong(DoanhThuTheoLoaiPhongBUS.LayDoanhThuTheoLoaiPhongCuoiCung().Id, 6, "MDTTLP");
            else                
                dttlp.Id = "MDTTLP1";

            dttlp.Thang = thang;
            dttlp.TongDoanhThu = float.Parse(txtTongDoanhThu.Text.ToString());

            DoanhThuTheoLoaiPhongBUS.ThemDoanhThuTheoLoaiPhong(dttlp);
            
            for (int i = 0; i < list_lp.Count; i++)
            {
                Chitietdoanhthutheoloaiphong ctdttlp = new Chitietdoanhthutheoloaiphong();
                if (ChiTietDoanhThuTheoLoaiPhongBUS.LayChiTietDoanhThuTheoLoaiPhongMoiNhat() != null)
                    ctdttlp.Id = ThamSoBUS.TaoMaTangTuDong(ChiTietDoanhThuTheoLoaiPhongBUS.LayChiTietDoanhThuTheoLoaiPhongMoiNhat().Id, 8, "MCTDTTLP");
                else
                    ctdttlp.Id = "MCTDTTLP1";

                ctdttlp.Doanhthutheoloaiphong = dttlp;
                ctdttlp.DoanhThu = float.Parse(doanhthu[i].ToString());
                ctdttlp.Loaiphong = list_lp[i];
                ctdttlp.TiLe = int.Parse(tile[i].ToString());

                ChiTietDoanhThuTheoLoaiPhongBUS.ThemChiTietDoanhThuTheoLoaiPhong(ctdttlp);
            }
            #endregion

            MessageBox.Show("Báo cáo thành công rồi !!!");
        }
    }
}
